import request from '@/utils/request'

const SongsAPI = {
    get_songs: (page: number, per_page: number) => {
        return request<any, GetSongsResult>({
            url: 'api/songs/get_songs',
            method: 'get',
            params: {
                page: page,
                per_page: per_page
            }
        })
    },
    get_song_url: (song_id: number) => {
        return request<any, GetSongUrlResult>({
            url: `api/songs/get_download_url2/${song_id}`,
            method: 'get'
        })
    },
    delete_song: (song_id: number, file_path: string) => {
        return request<any, any>({
            url: `api/songs/${song_id}/${file_path}`,
            method: 'delete'
        })
    },
    upload_by_url: (song_meta: SongMeta, source_url: string) => {
        return request<any, any>({
            url: 'api/songs/upload_by_url',
            method: 'post',
            data: {
                song_meta_info: song_meta,
                url: source_url
            }
        })
    },
    get_upload_url: (title: string, file_base_name: string) => {
        return request<any, GetUploadUrlResult>({
            url: `api/songs/get_upload_url/${title}.${file_base_name}`,
            method: 'get'
        })
    },
    insert_meta_info: (song_meta: SongMeta) => {
        return request<any, any>({
            url: 'api/songs/insert_meta_info',
            method: 'post',
            data: song_meta
        })
    },
    get_songs_by_title: (title: string) => {
        return request<any, SearchSongsResult>({
            url: `api/songs/get_songs_by_title`,
            method: 'get',
            params: {
                title: title
            }
        })
    },
    get_songs_by_artist: (artist: string) => {
        return request<any, SearchSongsResult>({
            url: `api/songs/get_songs_by_artist`,
            method: 'get',
            params: {
                artist: artist
            }
        })
    },
    search_songs_by_title: (title: string) => {
        return request<any, SearchSongsResult>({
            url: `api/songs/search_songs_by_title`,
            method: 'get',
            params: {
                title: title
            }
        })
    }

}

export default SongsAPI

export interface GetUploadUrlResult {
    url: string,
    file_path: string
}

export interface GetSongsResult {
    pagination: {
        page: number,
        pageSize: number,
        total: number
    },
    songs: SongMeta[]
}

export interface SearchSongsResult {
    songs: SongMeta[]
}

export interface SongMeta {
    album: string,
    artist: string,
    composer: string,
    created_at: string,
    file_path: string,
    genre: string,
    lyricist: string,
    lyrics: string,
    release_date: string,
    song_id: number,
    title: string,
    updated_at: string
}

export interface GetSongUrlResult {
    url: string
}